﻿namespace WMS6.Web.OrderManagement
{
    using System;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;
    using WMS6.Web.LoginServiceRef;
    using WMS6.Web.OrderDetailMgtServiceRef;

    public class SalesOrderDetail : BasePage
    {
        protected Button btnCancel;
        protected Button btnSave;
        protected Button btnSaveContinue;
        protected DropDownList ddlStatus;
        protected DropDownList ddlUomID;
        protected HtmlForm F3023;
        protected HtmlInputHidden hdnFormart;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnOrderID;
        protected HtmlInputHidden hdnOwnerID;
        protected HtmlInputHidden hdnPageType;
        protected HtmlInputHidden hdnSkuID;
        protected HtmlInputHidden hdnType;
        protected HtmlInputHidden hdnUntiPrice;
        protected HtmlHead Head1;
        protected HtmlImage imgPackID;
        protected HtmlImage imgSkuID;
        protected Label lblExtendedPrice;
        protected Label lblLineID;
        protected Label lblMsg;
        protected Label lblOriginalQty;
        protected Label lblPackingID;
        protected Label lblPickingInstructions;
        protected Label lblRemark;
        protected Label lblSalesOrderDetail;
        protected Label lblShippedQty;
        protected Label lblSkuDesrc;
        protected Label lblSkuID;
        protected Label lblStatus;
        protected Label lblUdf1;
        protected Label lblUdf10;
        protected Label lblUdf2;
        protected Label lblUdf3;
        protected Label lblUdf4;
        protected Label lblUdf5;
        protected Label lblUdf6;
        protected Label lblUdf7;
        protected Label lblUdf8;
        protected Label lblUdf9;
        protected Label lblUnitPrice;
        protected Label lblUomID;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected TextBox txtExtendedPrice;
        protected TextBox txtLineID;
        protected TextBox txtOriginalQty;
        protected TextBox txtPackID;
        protected TextBox txtPickingInstuctions;
        protected TextBox txtRemark;
        protected TextBox txtShippedQty;
        protected TextBox txtSkuDesrc;
        protected TextBox txtSkuID;
        protected TextBox txtUdf1;
        protected TextBox txtUdf10;
        protected TextBox txtUdf2;
        protected TextBox txtUdf3;
        protected TextBox txtUdf4;
        protected TextBox txtUdf5;
        protected TextBox txtUdf6;
        protected TextBox txtUdf7;
        protected TextBox txtUdf8;
        protected TextBox txtUdf9;
        protected TextBox txtUnitPrice;

        private void BindData2UomList(string packID)
        {
            try
            {
                base.BindUomList2DDL(this.ddlUomID, packID);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7600_012") + ex.Message;
                this.txtPackID.Focus();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            base.Server.Transfer(string.Format("SalesOrder.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "2", base.UrlEncode(this.hdnOrderID.Value.Trim())));
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.ddlStatus.SelectedValue.ToUpper() != "NEW")
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7100_029");
            }
            else if (string.IsNullOrEmpty(this.txtOriginalQty.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7100_025");
            }
            else if (string.IsNullOrEmpty(this.txtSkuID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7100_026");
            }
            else if (string.IsNullOrEmpty(this.txtPackID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7100_027");
            }
            else if (string.IsNullOrEmpty(this.ddlUomID.SelectedValue))
            {
                this.lblMsg.Text = "包装" + this.txtPackID.Text + " 未找到对应的计量单位！";
            }
            else
            {
                bool success = false;
                try
                {
                    success = this.Insert0rUpdateOrderDetail();
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7100_030") + ex.Message;
                    return;
                }
                if (success)
                {
                    base.Response.Redirect(string.Format("SalesOrder.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "6", base.UrlEncode(this.hdnOrderID.Value.Trim())), false);
                }
            }
        }

        protected void btnSaveContinue_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtOriginalQty.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7100_025");
            }
            else if (string.IsNullOrEmpty(this.txtSkuID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7100_026");
            }
            else if (string.IsNullOrEmpty(this.txtPackID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7100_027");
            }
            else if (string.IsNullOrEmpty(this.ddlUomID.SelectedValue))
            {
                this.lblMsg.Text = "包装" + this.txtPackID.Text + " 未找到对应的计量单位！";
            }
            else
            {
                bool success = false;
                try
                {
                    success = this.Insert0rUpdateOrderDetail();
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7100_028") + ex.Message;
                    return;
                }
                if (success)
                {
                    this.lblMsg.Text = this.txtLineID.Text + base.GetGlobalMsgResource("F7100_011");
                    this.txtLineID.Text = (int.Parse(this.txtLineID.Text.Trim()) + 10).ToString();
                    this.txtSkuID.Text = "";
                    this.txtPackID.Text = "";
                    this.ddlUomID.Items.Clear();
                    this.txtOriginalQty.Text = "";
                    this.txtExtendedPrice.Text = "";
                    this.txtPickingInstuctions.Text = "";
                    this.txtRemark.Text = "";
                    this.txtShippedQty.Text = "";
                    this.txtSkuDesrc.Text = "";
                    this.txtSkuID.Text = "";
                    this.txtUdf1.Text = "";
                    this.txtUdf10.Text = "";
                    this.txtUdf2.Text = "";
                    this.txtUdf3.Text = "";
                    this.txtUdf4.Text = "";
                    this.txtUdf5.Text = "";
                    this.txtUdf6.Text = "";
                    this.txtUdf7.Text = "";
                    this.txtUdf8.Text = "";
                    this.txtUdf9.Text = "";
                    this.txtUnitPrice.Text = "";
                }
            }
        }

        private void EnableButton(string status)
        {
            string CS40000 = status;
            if ((CS40000 != null) && (CS40000 == "Cancelled"))
            {
                this.btnSave.Enabled = false;
                this.btnSaveContinue.Enabled = false;
            }
        }

        private bool Insert0rUpdateOrderDetail()
        {
            bool success = false;
            SalesOrderDetailInfo salesOrderDetailInfo = null;
            double doubleTemp = 0.0;
            if (base.DataEntityViewState != null)
            {
                salesOrderDetailInfo = base.DataEntityViewState as SalesOrderDetailInfo;
            }
            else
            {
                salesOrderDetailInfo = new SalesOrderDetailInfo();
            }
            salesOrderDetailInfo.ExternOrderID = "";
            salesOrderDetailInfo.ExternLineID = "";
            salesOrderDetailInfo.AltSkuID = "";
            salesOrderDetailInfo.LineID = int.Parse(this.txtLineID.Text.Trim());
            salesOrderDetailInfo.OrderID = this.hdnOrderID.Value.Trim();
            salesOrderDetailInfo.OwnerID = this.hdnOwnerID.Value.Trim();
            salesOrderDetailInfo.Status = this.ddlStatus.SelectedValue;
            salesOrderDetailInfo.SkuID = this.txtSkuID.Text.Trim();
            if (!string.IsNullOrEmpty(this.txtExtendedPrice.Text))
            {
                salesOrderDetailInfo.ExtendedPrice = double.Parse(this.txtExtendedPrice.Text);
            }
            else
            {
                salesOrderDetailInfo.ExtendedPrice = 0.0;
            }
            if (!string.IsNullOrEmpty(this.txtUnitPrice.Text))
            {
                salesOrderDetailInfo.UnitPrice = double.Parse(this.txtUnitPrice.Text);
            }
            else
            {
                salesOrderDetailInfo.ExtendedPrice = 0.0;
            }
            if (double.TryParse(this.txtUnitPrice.Text.Trim(), out doubleTemp))
            {
                salesOrderDetailInfo.UnitPrice = double.Parse(this.txtUnitPrice.Text.Trim());
            }
            else
            {
                salesOrderDetailInfo.UnitPrice = doubleTemp;
            }
            if (double.TryParse(this.txtExtendedPrice.Text.Trim(), out doubleTemp))
            {
                salesOrderDetailInfo.ExtendedPrice = double.Parse(this.txtExtendedPrice.Text.Trim());
            }
            else
            {
                salesOrderDetailInfo.ExtendedPrice = doubleTemp;
            }
            salesOrderDetailInfo.PackID = this.txtPackID.Text.Trim();
            salesOrderDetailInfo.UomID = this.ddlUomID.SelectedValue;
            salesOrderDetailInfo.OriginalQty = decimal.Parse(this.txtOriginalQty.Text.Trim());
            salesOrderDetailInfo.ActualShipDate = DateTime.Now;
            salesOrderDetailInfo.Remark = this.txtRemark.Text.Trim();
            if (!string.IsNullOrEmpty(this.hdnType.Value) && (this.hdnType.Value == "edit"))
            {
                salesOrderDetailInfo.UpdatedBy = base.UserInfo.UserID;
                if (!string.IsNullOrEmpty(OrderDetailMgtServiceClientProxy.GetInstance().UpdateSalesOrderDetail(salesOrderDetailInfo)))
                {
                    success = true;
                }
                return success;
            }
            salesOrderDetailInfo.CreatedBy = base.UserInfo.UserID;
            salesOrderDetailInfo.UpdatedBy = base.UserInfo.UserID;
            if (!string.IsNullOrEmpty(OrderDetailMgtServiceClientProxy.GetInstance().InsertSalesOrderDetail(salesOrderDetailInfo)))
            {
                success = true;
            }
            return success;
        }

        private void LoadData()
        {
            this.txtLineID.Enabled = false;
            base.BindData2DDLBySysCode(this.ddlStatus, "OrderStatus");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                string orderID = base.Request.QueryString["ID"];
                this.hdnOrderID.Value = orderID;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["type"]))
            {
                string type = base.Request.QueryString["type"];
                this.hdnType.Value = type;
                if (type == "edit")
                {
                    this.btnSaveContinue.Enabled = false;
                }
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                string from = base.Request.QueryString["from"];
                this.hdnFrom.Value = from;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["LineID"]))
            {
                string LineID = base.Request.QueryString["LineID"];
                this.txtLineID.Text = LineID;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["OwnerID"]))
            {
                string ownerID = base.Request.QueryString["OwnerID"];
                this.hdnOwnerID.Value = ownerID;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["SkuID"]))
            {
                string SkuID = base.Request.QueryString["SkuID"];
                this.txtSkuID.Text = SkuID;
                this.hdnSkuID.Value = SkuID;
            }
            this.setSalesOrderDetail2From();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
                PreferenceInfo preferenceInfo = base.UserInfo.Preference;
                this.hdnFormart.Value = preferenceInfo.Digits_No;
            }
        }

        private void setSalesOrderDetail2From()
        {
            if (((!string.IsNullOrEmpty(this.hdnOrderID.Value) && !string.IsNullOrEmpty(this.txtLineID.Text)) && !string.IsNullOrEmpty(this.txtSkuID.Text)) && !string.IsNullOrEmpty(this.hdnOwnerID.Value))
            {
                SalesOrderDetailInfo salesOrderDetailInfo = OrderDetailMgtServiceClientProxy.GetInstance().GetSalesOrderDetailInfoByID(this.hdnOrderID.Value, this.txtLineID.Text);
                if (salesOrderDetailInfo != null)
                {
                    this.ddlStatus.SelectedValue = salesOrderDetailInfo.Status;
                    SkuInfo skuInfo = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(this.hdnOwnerID.Value, this.txtSkuID.Text, "", "SKU") as SkuInfo;
                    if (!(((skuInfo == null) || string.IsNullOrEmpty(skuInfo.SkuID)) || string.IsNullOrEmpty(skuInfo.OwnerID)))
                    {
                        this.txtSkuDesrc.Text = skuInfo.Descr;
                        this.txtPackID.Text = skuInfo.PackID;
                        this.BindData2UomList(this.txtPackID.Text.Trim());
                    }
                    this.txtOriginalQty.Text = base.GetPreferenceDigit(salesOrderDetailInfo.OriginalQty);
                    this.txtExtendedPrice.Text = base.GetPreferenceDigit(salesOrderDetailInfo.ExtendedPrice);
                    this.txtShippedQty.Text = base.GetPreferenceDigit(salesOrderDetailInfo.ShippedQty);
                    this.txtUnitPrice.Text = base.GetPreferenceDigit(salesOrderDetailInfo.UnitPrice);
                    this.hdnUntiPrice.Value = base.GetPreferenceDigit(salesOrderDetailInfo.UnitPrice);
                    this.txtUdf1.Text = salesOrderDetailInfo.Udf1;
                    this.txtUdf2.Text = salesOrderDetailInfo.Udf2;
                    this.txtUdf3.Text = salesOrderDetailInfo.Udf3;
                    this.txtUdf4.Text = salesOrderDetailInfo.Udf4;
                    this.txtUdf5.Text = salesOrderDetailInfo.Udf5;
                    this.txtUdf6.Text = salesOrderDetailInfo.Udf6;
                    this.txtUdf7.Text = salesOrderDetailInfo.Udf7;
                    this.txtUdf8.Text = salesOrderDetailInfo.Udf8;
                    this.txtUdf9.Text = salesOrderDetailInfo.Udf9;
                    this.txtUdf10.Text = salesOrderDetailInfo.Udf10;
                    this.txtRemark.Text = salesOrderDetailInfo.Remark;
                    base.DataEntityViewState = salesOrderDetailInfo;
                    this.EnableButton(this.ddlStatus.SelectedValue);
                }
            }
        }

        protected void txtOriginalQty_OnTextChanged(object sender, EventArgs e)
        {
            if (!(string.IsNullOrEmpty(this.txtOriginalQty.Text) || !(this.txtUnitPrice.Text != "0")))
            {
                this.txtExtendedPrice.Text = (decimal.Parse(this.txtOriginalQty.Text) * decimal.Parse(this.txtUnitPrice.Text)).ToString();
            }
            else
            {
                this.txtExtendedPrice.Text = base.GetPreferenceDigit("0");
            }
        }

        protected void txtPackID_TextChanged(object sender, EventArgs e)
        {
            this.BindData2UomList(this.txtPackID.Text.Trim());
        }

        protected void txtSkuID_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string skuID = this.txtSkuID.Text.Trim();
                if (string.IsNullOrEmpty(skuID))
                {
                    this.txtSkuDesrc.Text = "";
                    this.txtPackID.Text = "";
                    this.txtUnitPrice.Text = "";
                    this.hdnUntiPrice.Value = "0";
                    this.txtExtendedPrice.Text = "0";
                    this.ddlUomID.Items.Clear();
                }
                else
                {
                    SkuInfo skuInfo = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(this.hdnOwnerID.Value, skuID, "", "SKU") as SkuInfo;
                    if ((((skuInfo != null) && !string.IsNullOrEmpty(skuInfo.SkuID)) && !string.IsNullOrEmpty(skuInfo.OwnerID)) && (skuInfo.IsActive == "Y"))
                    {
                        this.txtSkuDesrc.Text = skuInfo.Descr;
                        this.txtPackID.Text = skuInfo.PackID;
                        if (skuInfo.SalePrice > 0M)
                        {
                            this.txtUnitPrice.Text = base.GetPreferenceDigit(skuInfo.SalePrice);
                        }
                        else
                        {
                            this.txtUnitPrice.Text = base.GetPreferenceDigit("0");
                        }
                        this.BindData2UomList(this.txtPackID.Text.Trim());
                        this.txtOriginalQty.Focus();
                        this.txtUnitPrice.Text = base.GetPreferenceDigit(skuInfo.SalePrice.ToString());
                        this.hdnUntiPrice.Value = base.GetPreferenceDigit(skuInfo.SalePrice.ToString());
                        if (!string.IsNullOrEmpty(this.txtOriginalQty.Text.Trim()))
                        {
                            this.txtExtendedPrice.Text = base.GetPreferenceDigit((float.Parse(skuInfo.SalePrice.ToString()) * float.Parse(this.txtOriginalQty.Text.Trim())).ToString());
                        }
                    }
                    else
                    {
                        this.lblMsg.Text = this.txtSkuID.Text + "  " + base.GetGlobalMsgResource("F7600_013");
                        this.txtSkuID.Text = "";
                        this.txtSkuID.Focus();
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>CloseSearch();false;</script>", false);
            }
        }

        protected void txtUnitPrice_TextChanged(object sender, EventArgs e)
        {
            double doubleTemp = 0.0;
            if (double.TryParse(this.txtOriginalQty.Text.Trim(), out doubleTemp) && double.TryParse(this.txtUnitPrice.Text.Trim(), out doubleTemp))
            {
                this.txtExtendedPrice.Text = (double.Parse(this.txtOriginalQty.Text.Trim()) * double.Parse(this.txtUnitPrice.Text.Trim())).ToString();
            }
            this.hdnUntiPrice.Value = this.txtUnitPrice.Text.Trim();
        }
    }
}

